﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionePolveri2.DAL;
using GestionePolveriLib.BL.Reports;
using FrameworkLib.Facades;

namespace GestionePolveriLib.Facades.Reports
{
    public class Report512Facade : FacadeBase<GestionePolveriEntities>
    {
        Filter512 m_filter = null;

        public Report512Facade(Filter512 filter)
        {
            m_filter = filter;
        }

        public List<Report512> GetReport512Data()
        {
            List<Report512> retValue = null;

            retValue = GetReport521Default2();

            return retValue;
        }

        private List<Report512> GetReport521Default2()
        {
            List<Report512> retValue = null;

            try
            {
                GestionePolveriEntities context = this.GetContext();

                var obj = context.Movimenti
                        .Where(m_filter.SetUpFilter())
                        .Join(context.StoricoMovimenti,
                              m => m.IDMovimento,
                              sm => sm.IDMovimento,
                              (m, sm) => new Report512
                              {
                                  ID = m.IDMovimento,
                                  Causale = m.CausaliMagazzino.Descrizione,
                                  CodicePolvere = m.AnagPolveri.CodicePolvere,
                                  DataMovimento = m.DataMovimento,
                                  NumScatole = m.NumScatole,
                                  Operatore = m.aspnet_Users.UserName,
                                  Quantita = m.Quantita,
                                  SottoScorta = sm.Sottoscorta,
                                  GiacenzaInsufficiente = sm.GiacenzaNegativa,
                                  ColoriAssociati = "",
                                  Giacenza=sm.Giacenza,
                                  Valore=sm.Valore,
                              }); 


                //var query = database.Posts    // your starting point
                //           .Join(database.Post_Metas, // the source table of the inner join
                //              post => post.ID,        // Select the primary key (the first part of the "on" clause in an sql "join" statement)
                //              meta => meta.Post_ID,   // Select the foreign key (the second part of the "on" clause)
                //              (post, meta) => new { Post = post, Meta = meta }) // selection


                //var obj1 = (from x in context.Movimenti
                //           join y in context.StoricoMovimenti on x.IDMovimento equals y.IDMovimento
                //           //where m_filter.SetUpFilter()
                //           select new Report512
                //           {
                //               ID = x.IDMovimento,
                //               Causale = x.CausaliMagazzino.Descrizione,
                //               CodicePolvere = x.AnagPolveri.CodicePolvere,
                //               DataMovimento = x.DataMovimento,
                //               NumScatole = x.NumScatole,
                //               Operatore = x.aspnet_Users.UserName,
                //               Quantita = x.Quantita,
                //               SottoScorta = y.Sottoscorta,
                //               GiacenzaInsufficiente = y.GiacenzaNegativa,
                //               ColoriAssociati = "",
                //           });

                if (obj.Any())
                {
                    retValue = obj.ToList();

                    MovimentiFacade mf = new MovimentiFacade();
                    
                    foreach (Report512 item in retValue)
                    {
                        item.ColoriAssociati = mf.GetStoricoColoriMovimento(item.ID);
                    }
                }
            }
            catch (Exception ex)
            { 

            }

            return retValue;
        }
    }
}
